function hide(alg){
  var formData = new FormData();
  var xhr = new XMLHttpRequest();

  var file1 = document.getElementsByName('file1')[0].files[0];
  var secret_file = document.getElementsByName('secret_file')[0].files[0];
  var secret = document.getElementsByName('secret_text')[0].value;

  formData.append('file1', file1);
  formData.append('secret_file', secret_file);
  formData.append('secret_text', secret);
  if (alg=="lsb") {
    var bits = document.getElementsByName('bits')[0].value;
    formData.append('bits', bits);
    xhr.open('POST', '/handle-lsb-hide', true);
  } else if(alg=="pvd") {
    var kuangdu = document.getElementsByName('pvdkuangdu')[0].value;
    formData.append('pvdkuangdu', kuangdu);
    xhr.open('POST', '/handle-pvd-hide', true);
  } else if(alg=="dct") {
    var kuangdu = document.getElementsByName('juzhen_kuang')[0].value;
    formData.append('juzhen_kuang', kuangdu);
    xhr.open('POST', '/handle-dct-hide', true);
  }

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var response = JSON.parse(xhr.responseText);
      if (response.error == '1') {
        window.location = "/error.html?message="+response.message
      }
      else if(response.error == '0'){
        window.location = "/image_ok.html?id="+response.id
      }
      console.log(response);
    }
  };
  xhr.send(formData);                                           
}

function extract(alg){
  var formData = new FormData();
  var xhr = new XMLHttpRequest();

  var file2 = document.getElementsByName('file2')[0].files[0];

  formData.append('file2', file2)

  if (alg=="lsb") {
    var bits = document.getElementsByName('bits')[0].value;
    formData.append('bits', bits)
    xhr.open('POST', '/handle-lsb-extract', true);
  } else if(alg=="pvd") {
    
    var source = new EventSource("/progress");
    source.onmessage = function(event) {
      let div = document.querySelector('#flask_progress')
      let text = document.getElementById('flask_progress')
      div.style.width = event.data+'%'
      text.innerHTML = event.data+'%'
      console.log(event.data)
      if(event.data > 100) {
        div.style.width = 100 + '%'
        text.innerHTML = 100 + '%'
        source.close ()
      }
    }
    
    var kuangdu = document.getElementsByName('pvdkuangdu')[0].value;
    formData.append('pvdkuangdu', kuangdu);
    xhr.open('POST', '/handle-pvd-extract', true);
  } else if(alg=="dct") {
    var kuangdu = document.getElementsByName('juzhen_kuang')[0].value;
    formData.append('juzhen_kuang', kuangdu);
    xhr.open('POST', '/handle-dct-extract', true);
  }

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var response = JSON.parse(xhr.responseText);
      if (response.error == '1') {
        window.location = "/error.html?message="+response.message
      }
      else if(response.error == '0'){
        document.getElementById("text-extract").innerHTML = response.secrets;
      }
      console.log(response);
    }
  };
  xhr.send(formData);                                           
}
function clearData() {
  document.getElementById("text").value = "";
  showNotification("信息已清除");
}

function copyData() {
  var dataToCopy = document.getElementById("text").value;
  navigator.clipboard.writeText(dataToCopy);
  showNotification("信息已复制到剪贴板");
}

function showNotification(message) {
  var notification = document.createElement('div');
  notification.classList.add('notification');
  notification.textContent = message;
  document.body.appendChild(notification);
  setTimeout(function() {
    document.body.removeChild(notification);
  }, 2000); // 2秒后自动移除通知
}
window.onload = function () {
  // 获取文本输入框和文件输入框的元素
  var secretTextInput = document.querySelector('input[name="secret_text"]');
  var secretFileInput = document.querySelector('input[name="secret_file"]');

  // 当文本输入框的值发生变化时，显示文本输入框，隐藏文件输入框
  secretTextInput.addEventListener("input", function() {
    secretTextInput.style.display = "block";
    secretFileInput.style.display = "none";
  });

  // 当文件输入框的值发生变化时，显示文件输入框，隐藏文本输入框
  secretFileInput.addEventListener("change", function() {
    secretFileInput.style.display = "block";
    secretTextInput.style.display = "none";
  });
  var oBox = document.getElementById("uploadfile");
  var fileInput = document.getElementById("fileInput");

  oBox.ondragenter = function(e){
    e.preventDefault();
    // console.log(e);
  }
  oBox.ondragover = function(e){
    e.preventDefault();
    // console.log(e);
  }
  oBox.ondrop = function(e){
    e.preventDefault();
    console.log(e);
    fileInput.files = e.dataTransfer.files;
  }
}
